<?php
//+=============================
//|  会员管理系统
//|  time: 2016-06-19 By One
//|  *^_^*
//+=============================

namespace Home\Controller;
use Think\Controller;

class UserController extends Controller
{
    public function index()
    {
        // $nav = $this->nav();
        // $this->assign("nav",$nav);
        // return $this->fetch();
    }

    //+-----------------------
    //|  会员登录
    //|  time: 2016-06-19 By One
    //+-----------------------
    public function login()
    {
        if($_POST){
        $username = I("post.username"); 
        $password = I("post.password"); 
        $verify = I("post.verify"); 

        $Model = D("Common");
        $d = $Model->check_verify($verify);
            
        if(!$d){
            $data["info"] = "验证码错误！";

            $this->ajaxReturn($data);die;
        }


        $user = D("user");
        $arr = $user->login($username,$password);


            if(!empty($arr)){
                $data['info'] = 1;
                session("username",$arr["username"]);
                session("user_id",$arr["id"]);
                session("pid",$arr["pid"]);
            }else{
                $data['info'] = "登录失败，用户名或者密码错误！";
            }

       

        $this->ajaxReturn($data);

        }else{
            $this->display();
        }
    }

    //+-----------------------
    //|  查询会员
    //|  time: 2016-06-19 By One
    //+-----------------------
    public function select()
    {
        $user = D("user");
        $arr  = $user->getAllUserInfo();
        return $arr;
    }

    //+-----------------------
    //|  添加会员
    //|  time: 2016-06-19 By One
    //+-----------------------
    public function add()
    {   
        if($_POST){

            $verify = D("Common");

            //验证码是否正确
            $d = $verify->checkSMS($_POST["verify"]);
                
            if(!$d){
                $data["info"] = "验证码错误！";
                $this->ajaxReturn($data);
            }

            $Model = D("user");
          //验证用户名是否存在
            if (!$Model->create()){
                    $data['info'] = $Model->getError();
            }else{
                $post = $Model->create();
                $return = $Model->add($post);   
                if($return){
                    $data['info'] = 1;
                    session("username",$post['username']);
                }else{
                    $data['info'] = "注册失败！";
                }

            }
            $this->ajaxReturn($data);
        }else{
            $this->display("login");
        }


    }

    //+-----------------------
    //|  删除会员
    //|  time: 2016-06-19 By One
    //+-----------------------
    public function delete()
    {

    }

    //+-----------------------
    //|  编辑会员
    //|  time: 2016-06-19 By One
    //+-----------------------
    public function edit()
    {

    }

    //+-----------------------
    //|  保存会员
    //|  time: 2016-06-19 By One
    //+-----------------------
    public function save()
    {

    }

    //+-----------------------
    //|  会员退出
    //|  time: 2016-06-19 By One
    //+-----------------------
    public function logout()
    {
        session(NULL);
        $this->redirect("user/login");
    }
}
